package heartbeat;

import sdm.repackaged.org.json.JSONException;
import sdm.repackaged.org.json.JSONObject;
import sdm.repackaged.org.json.JSONTokener;

public class PacketUtils 
{
	static JSONObject stringToJSON(String source)
	{
		try 
		{
			return new JSONObject(new JSONTokener(source));
		} 
		catch (JSONException e) 
		{
			System.out.println("JSON ERROR: " + source);
			e.printStackTrace();
			return null;
		}
	}
	
	static String packetToString(Packet p)
	{
		try
		{
			int channel = p.channel();
			JSONObject pMessage = p.message();
			JSONObject pHeaderJson = new JSONObject().put("channel", channel);
			JSONObject pOutJson = new JSONObject().put("header", pHeaderJson);
			pOutJson.put("message", pMessage);
			return pOutJson.toString();
		}
		catch(JSONException e)
		{
			return null;
		}
	}
}
